package com.inei.cie.capituloviii;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVIIIDao;
import com.inei.cie.entidades.P8;

public class CapituloVIII_Muro_De_Contencion_Activity extends Activity {

	public Spinner sp6F84, sp6F85, sp6F86, sp6F87;
	public TableLayout tl6Fmuro;
	public TableRow tr6F84, tr6F84texto;
	public EditText et6F84otro, et6F81, et6F82, et6F83, et6F8,
			et6FObservaciones;

	public Bundle b;
	public TextView tvOtroEdificacion;
	public String Titulo, Tipo;
	public int Otro;
	public int cod;

	// Data
	public CapituloVIIIDao cap8dao;
	public String id_local;
	public int Nro_Pred;
	public String edificacion;
	public P8 obj;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_viii_muro_de_contencion);

		IniciarComponentes();

		sp6F84.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				switch (pos) {
				case 7:
					tl6Fmuro.removeView(tr6F84texto);
					tl6Fmuro.addView(tr6F84texto,
							tl6Fmuro.indexOfChild(tr6F84) + 1);
					et6F84otro.setText("");
					et6F84otro.setText(obj.getP8_ejecuto_O());
					break;

				default:
					// et6F84otro.setText("");
					tl6Fmuro.removeView(tr6F84texto);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void IniciarComponentes() {
		tvOtroEdificacion = (TextView) findViewById(R.id.tvOtroEdificacion);

		tl6Fmuro = (TableLayout) findViewById(R.id.tl6Fmuro);
		tr6F84 = (TableRow) findViewById(R.id.tr6F84);
		tr6F84texto = (TableRow) findViewById(R.id.tr6F84texto);

		et6F8 = (EditText) findViewById(R.id.et6F8);
		et6F8.setEnabled(false);
		et6F81 = (EditText) findViewById(R.id.et6F81);
		et6F82 = (EditText) findViewById(R.id.et6F82);
		et6F83 = (EditText) findViewById(R.id.et6F83);
		et6F84otro = (EditText) findViewById(R.id.et6F84otro);
		et6FObservaciones = (EditText) findViewById(R.id.et6FObservaciones);

		sp6F84 = (Spinner) findViewById(R.id.sp6F84);
		sp6F85 = (Spinner) findViewById(R.id.sp6F85);
		sp6F86 = (Spinner) findViewById(R.id.sp6F86);
		sp6F87 = (Spinner) findViewById(R.id.sp6F87);

		tl6Fmuro.removeView(tr6F84texto);

		et6F83.setEnabled(false);

		cargarData();
	}

	public void cargarData() {
		b = getIntent().getExtras();
		id_local = b.getString("id_local");
		Nro_Pred = b.getInt("Nro_Pred");
		cod = b.getInt("Codigo");
		Otro = b.getInt("TipoOtro");

		if (Otro == 1) {
			Titulo = "Patios";
		} else if (Otro == 2) {
			Titulo = "Losas Deportivas";
		} else if (Otro == 3) {
			Titulo = "Cisternas - Tanques";
		} else if (Otro == 4) {
			Titulo = "Muros de contenci\u00f3n";
		}

		tvOtroEdificacion.setText("Local Nro.  "
				+ id_local
				+ " - Predio : "
				+ (getIntent().getExtras().getInt("TITULO_PRED") < 10 ? "0"
						: "") + getIntent().getExtras().getInt("TITULO_PRED")
				+ " - " + Titulo);

		String co = cod + "";
		et6F8.setText((co.length() == 1 ? "0" : "") + cod);

		cap8dao = new CapituloVIIIDao(getApplicationContext());
		obj = cap8dao.obtenerP8(id_local, Nro_Pred, cod, 4);
		et6F81.setText(obj.getP8_longitud());
		et6F82.setText(obj.getP8_altura());

		// String pr = obj.getNro_Pred() + "";
		String pr = getIntent().getExtras().getInt("TITULO_PRED") + "";
		// et6F83.setText((pr.length() == 1 ? "0" : "") + obj.getNro_Pred());
		et6F83.setText((pr.length() == 1 ? "0" : "")
				+ getIntent().getExtras().getInt("TITULO_PRED"));

		if (obj.getP8_ejecuto() != 0) {
			sp6F84.setSelection(obj.getP8_ejecuto());
		}
		if (obj.getP8_ejecuto() == 7) {
			et6F84otro.setText(obj.getP8_ejecuto_O());
		}
		if (obj.getP8_Est_E() != 0) {
			sp6F85.setSelection(obj.getP8_Est_E());
		}
		if (obj.getP8_Ant() != 0) {
			sp6F86.setSelection(obj.getP8_Ant());
		}
		if (obj.getP8_RecTec() != 0) {
			sp6F87.setSelection(obj.getP8_RecTec());
		}

		et6FObservaciones.setText(obj.getP8_Obs());
	}

	public void guardar() {
		obj.setId_local(id_local);
		obj.setP8_2("MC - " + cod);
		obj.setP8_longitud(et6F81.getText().toString());
		obj.setP8_altura(et6F82.getText().toString());
		// obj.setNro_Pred(Integer.parseInt(et6F83.getText().toString()));
		obj.setP8_ejecuto(sp6F84.getSelectedItemPosition());
		obj.setP8_ejecuto_O(et6F84otro.getText().toString());
		obj.setP8_Est_E(sp6F85.getSelectedItemPosition());
		obj.setP8_Ant(sp6F86.getSelectedItemPosition());
		obj.setP8_RecTec(sp6F87.getSelectedItemPosition());
		obj.setP8_Obs(et6FObservaciones.getText().toString());
		obj.setSwCompleto(true);
		cap8dao.actualizarP8(obj, 4);

		Intent irMenu = new Intent(this,
				CapituloVIII_Listado_General_Activity.class);
		irMenu.putExtras(getIntent().getExtras());
		startActivity(irMenu);

		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capituloviii, menu);
		menu.getItem(0).setTitle("MUROS");
		menu.getItem(1).setTitle("TERMINAR");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnMarco:
			Intent irMarco = new Intent(this, Carga_Trabajo_Activity.class);
			irMarco.putExtras(getIntent().getExtras());
			startActivity(irMarco);
			finish();
			break;
		case R.id.mnIncio:
			Intent marco = new Intent(this,
					CapituloVIII_Listado_General_Activity.class);
			marco.putExtras(getIntent().getExtras());
			startActivity(marco);
			finish();
			break;
		case R.id.mnSiguiente:
			String p8_1 = et6F81.getText().toString();
			String p8_2 = et6F82.getText().toString();
			int p8_4 = sp6F84.getSelectedItemPosition();
			String p8_4_o = et6F84otro.getText().toString();
			int p8_5 = sp6F85.getSelectedItemPosition();
			int p8_6 = sp6F86.getSelectedItemPosition();
			int p8_7 = sp6F87.getSelectedItemPosition();

			if (p8_1.length() != 0 && p8_2.length() != 0 && p8_4 != 0
					&& p8_5 != 0 && p8_6 != 0 && p8_7 != 0) {
				if (p8_4 == 7) {
					if (p8_4_o.trim().length() != 0) {
						guardar();
					} else {
						Toast.makeText(
								this,
								"Debe especificar otra instituci\u00f3n, organismo o empresa",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					guardar();
				}

			} else {
				Toast.makeText(this, "Debe responder todas las preguntas",
						Toast.LENGTH_SHORT).show();
			}

			break;
		case R.id.mnCalculadora:
			Intent i = new Intent();
			i.setClassName("com.android.calculator2",
					"com.android.calculator2.Calculator");
			startActivity(i);
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(
					CapituloVIII_Muro_De_Contencion_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(this,
				CapituloVIII_Listado_General_Activity.class);
		irPredio.putExtras(getIntent().getExtras());
		startActivity(irPredio);
		finish();
	}
}
